package com.telit.uav.controller;

import com.telit.common.entity.DataReturn;
import com.telit.common.enums.ResultCode;
import com.telit.uav.dto.TeamDTO;
import com.telit.uav.dto.TeamDeleteDTO;
import com.telit.uav.service.TeamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @Auther amy
 * @Describe 队伍控制层
 */
@RestController
@Api(tags = "飞手队伍管理",description = "/team")
@RequestMapping("/team")
public class TeamController {

    @Autowired
    private TeamService teamService;

    @GetMapping(value = "/listNoPage",produces = "application/json;charset=UTF-8")
    @ApiOperation("队伍无分页查询")
    public DataReturn list(@RequestParam(required = false,value = "teamName") String teamName,
                           @RequestParam(defaultValue = "1",value = "teamType")String teamType){
        return DataReturn.ok(teamService.listNoPage(teamName,teamType));
    }

    @PostMapping(value = "/add",produces = "application/json;charset=UTF-8")
    @ApiOperation("添加")
    public DataReturn add(@Validated @RequestBody TeamDTO teamDTO){
        if(teamService.add(teamDTO) <= 0){
            return DataReturn.failure(ResultCode.OPERATE_ERROR,"新增队伍失败");
        }
        return DataReturn.ok();
    }

    @PostMapping(value = "/remove",produces = "application/json;charset=UTF-8")
    @ApiOperation("删除")
    public DataReturn remove(@Validated @RequestBody TeamDeleteDTO teamDeleteDTO){
        teamService.remove(teamDeleteDTO);
        return DataReturn.ok();
    }

    @PostMapping(value = "/edit",produces = "application/json;charset=UTF-8")
    @ApiOperation("修改")
    public DataReturn edit(@Validated @RequestBody TeamDTO teamDTO){
        if(teamService.update(teamDTO) <= 0){
            return DataReturn.failure(ResultCode.OPERATE_ERROR,"修改队伍失败");
        }
        return DataReturn.ok();
    }

    @GetMapping(value = "/detail/{id}",produces = "application/json;charset=UTF-8")
    @ApiOperation("队伍详情,只返回用户id")
    public DataReturn detail(@PathVariable("id")Long id){
        return DataReturn.ok(teamService.detail(id));
    }

    @GetMapping(value = "/memberDetailByPage/{id}",produces = "application/json;charset=UTF-8")
    @ApiOperation("队伍成员详情-分页查询")
    public DataReturn userDetail(@RequestParam(defaultValue = "1") Integer current,
                               @RequestParam(defaultValue = "10") Integer size,
                               @PathVariable(value = "id") Long id,
                               @RequestParam(value = "queryName",required = false)String queryName){
        return DataReturn.ok(teamService.memberDetailByPage(current,size,id,queryName));
    }

    @GetMapping(value = "/noTeamList",produces = "application/json;charset=UTF-8")
    @ApiOperation("飞手列表(不在指定队伍中或者不在队伍中),teamId如果传值,返回的用户列表就会包括该队伍中的用户,用于区分新增/修改选择人数")
    public DataReturn uavUserList(@RequestParam(value = "teamId",required = false)Long teamId){
        return DataReturn.ok(teamService.excludeTeamUser(teamId));
    }

    @GetMapping(value = "/selectByUserId",produces = "application/json;charset=UTF-8")
    @ApiOperation("根据用户id查询队伍信息")
    public DataReturn selectByUserId(@RequestParam(defaultValue = "1",value = "teamType")String teamType,
                                     @RequestParam(value = "userId")Long userId){
        return DataReturn.ok(teamService.selectByUserId(userId,teamType));
    }
}
